<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 6] 6.11 The throw Statement</TITLE>
<META NAME="author" CONTENT="Mark Grand">
<META NAME="date" CONTENT="Thu Jul 31 13:14:43 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="java">
<META NAME="title" CONTENT="Java Language Reference">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java Language Reference" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch06_10.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 6<br>Statements and Control Structures</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch06_12.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JLR2-CH-6-SECT-11">6.11 The throw Statement</A></h2>

<P CLASS=para>
<A NAME="CH06.THROW"></A><A NAME="CH06.THROW2"></A>A <tt CLASS=literal>throw</tt> statement is used to cause
an exception to be thrown:


<p>
<img align=middle src="./figs/jlr0620.gif" alt="[Graphic: Figure from the text]" width=450 height=82 border=0>

<P CLASS=para>
The expression in a <tt CLASS=literal>throw</tt> statement
must produce a reference to an object that is an instance of the
<tt CLASS=literal>Throwable</tt> class or one of its subclasses. Otherwise,
the compiler issues an error message. You typically want the expression
in a <tt CLASS=literal>throw</tt> statement to produce an object that
is an instance of a subclass of the <tt CLASS=literal>Exception</tt>
class.

<P CLASS=para>
Here is an example of a <tt CLASS=literal>throw</tt> statement:

<DIV CLASS=screen>
<P>
<PRE>
throw new ProtocolException();
</PRE>
</DIV>

<P CLASS=para>
<A NAME="CH06.TRY"></A>A <tt CLASS=literal>throw</tt> statement
causes normal program execution to stop. Control is immediately
transferred to the innermost enclosing <tt CLASS=literal>try</tt> statement
in the search for a <tt CLASS=literal>catch</tt> clause that can handle
the exception. If the innermost <tt CLASS=literal>try</tt> statement
cannot handle the exception, the exception propagates up through
enclosing statements in the current method. If the current method
does not contain a <tt CLASS=literal>try</tt> statement that can handle
the exception, the exception propagates up to the invoking method.
If this method does not contain an appropriate
<tt CLASS=literal>try</tt> statement,
the exception propagates up again, and so on. Finally, if no
<tt CLASS=literal>try</tt> statement is found to handle the exception,
the currently running thread terminates. The termination of a thread
is described in <A HREF="ch08_01.htm#JLR2-CH-8-SECT-1.2.1">Stopping a thread</A>.

<P CLASS=para>
As an exception propagates through
enclosing <tt CLASS=literal>try</tt> statements, any
<tt CLASS=literal>finally</tt> blocks associated with those
<tt CLASS=literal>try</tt> statements
are executed until the exception is caught. If a <tt CLASS=literal>finally</tt> block contains a <tt CLASS=literal>break</tt>,
<tt CLASS=literal>continue</tt>,
<tt CLASS=literal>return</tt>, or <tt CLASS=literal>throw</tt> statement,
the pending control transfer initiated by the <tt CLASS=literal>throw</tt>
statement is forgotten. Instead, control is transferred to the target of the
<tt CLASS=literal>break</tt>, <tt CLASS=literal>continue</tt>,
<tt CLASS=literal>return</tt>,
or <tt CLASS=literal>throw</tt> statement in the <tt CLASS=literal>finally</tt> block.

<P CLASS=para>
<b>References</b>
Exception Handling 9;
<I CLASS=emphasis>Expression</I> 4;
<A HREF="ch06_08.htm#JLR2-CH-6-SECT-8">The break Statement</A>;
<A HREF="ch06_09.htm#JLR2-CH-6-SECT-9">The continue Statement</A>;
<A HREF="ch06_10.htm#JLR2-CH-6-SECT-10">The return Statement</A>;
<A HREF="ch06_12.htm#JLR2-CH-6-SECT-12">The try Statement</A>;
<A HREF="ch10_25.htm">Throwable</A>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch06_10.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch06_12.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>The return Statement</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>The try Statement</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
